/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.supply;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import main.WinkelApplication;
import model.Category;
import model.Product;

/**
 *
 * @author ShalDDA
 */
public class SupplyEdit extends JPanel {
    private int prod_id;
    private TextField naamText;
    private TextField prijsText;
    private TextField omschrijvingText;
    private TextField voorraadText;
    private JComboBox ComboCat;
    
    SupplyEdit(int prod_id) {
        super();
        this.prod_id = prod_id;
        toSetLayout();
        
    }
    
    public Product getProduct() {
        Product product = new Product();
        product = WinkelApplication.getQueryManager().getProduct(this.prod_id);
        
        return product;
    }

    private void toSetLayout() {
        setLayout(new BorderLayout());
        add(getTitle(), BorderLayout.PAGE_START);
        add(pnlCompenents(getProduct()), BorderLayout.CENTER);
        add(getPnlButtons(), BorderLayout.PAGE_END);
        
    }

    private JLabel getTitle() {
        JLabel title = new JLabel("Product wijzigen", SwingConstants.CENTER);
        title.setFont(WinkelApplication.computeFontSize(2));
        
        return title;
    }

    private JPanel pnlCompenents(Product product) {
        FormLayout layout = new FormLayout(
        "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, " +
        "right:max(40dlu;pref), 3dlu, 70dlu",
        "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
        "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
        "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +      
         "p, 3dlu, p, 3dlu, p, 3dlu, p" );
                
        JPanel panel = new JPanel(layout);
        panel.setBorder(Borders.DIALOG_BORDER);
       
        List<Category> catIdList = WinkelApplication.getQueryManager().getCategories();
        String[] catIdArray2 = new String[catIdList.size()];
     
        for(int i = 0; i < catIdList.size(); i++) {
            Category category = catIdList.get(i);
            catIdArray2[category.getCategoryId()-1] = category.getName() ;
        }
        
        ComboCat = new JComboBox(catIdArray2);
        ComboCat.setSelectedIndex(product.getCategorieId()-1);
        naamText = new TextField();
        prijsText = new TextField();
        omschrijvingText = new TextField();
        voorraadText = new TextField();
        
        JLabel catId = new JLabel("Catergorie-ID");
        JLabel naam = new JLabel("Naam");
        naamText.setText(product.getName());
        JLabel prijs = new JLabel("Prijs");
        prijsText.setText(String.valueOf(product.getPrice()));
        JLabel omschrijving = new JLabel("Omschrijving");
        omschrijvingText.setText(product.getDescription());
        JLabel voorraad = new JLabel("Voorraad");
        voorraadText.setText(String.valueOf(product.getVoorraad()));
        
        catId.setFont(WinkelApplication.computeFontSize(3));
        naam.setFont(WinkelApplication.computeFontSize(3));
        prijs.setFont(WinkelApplication.computeFontSize(3));
        omschrijving.setFont(WinkelApplication.computeFontSize(3));
        voorraad.setFont(WinkelApplication.computeFontSize(3));
        
        // Fill the table with labels and components.
        CellConstraints cc = new CellConstraints();
        
        panel.add(catId,             cc.xy (1, 3));
        panel.add(ComboCat,         cc.xyw(3, 3, 3));
        panel.add(naam,              cc.xy (1, 5));
        panel.add(naamText,          cc.xyw(3, 5, 3));
        panel.add(prijs,             cc.xy (1, 7));
        panel.add(prijsText,         cc.xyw(3, 7, 3));
        panel.add(omschrijving,      cc.xy (1, 9));
        panel.add(omschrijvingText,  cc.xyw(3, 9, 3)); 
        panel.add(voorraad,          cc.xy (1, 11));
        panel.add(voorraadText,      cc.xyw(3, 11, 3));
        
        return panel; 
    }

    private JPanel getPnlButtons() {
        JPanel panel = new JPanel();
       
        
        JButton terug = new JButton("Annuleren");
        
        terug.setPreferredSize(new Dimension(150,25));
        terug.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {
            javax.swing.SwingUtilities.getWindowAncestor(SupplyEdit.this).dispose();
          }
        });
        
        JButton opslaan = new JButton("Opslaan");
        
        opslaan.setPreferredSize(new Dimension(150,25));
        opslaan.addMouseListener(new MouseAdapter() {
                  
          @Override
          public void mouseClicked(MouseEvent e) {
           //int prod_id = prod_id;
            int cat_id = ComboCat.getSelectedIndex()+1;
            double prijs = Double.parseDouble(prijsText.getText());
            int voorraad = Integer.parseInt(voorraadText.getText());

            
            addProduct( prod_id, cat_id, naamText.getText(),
                        prijs, omschrijvingText.getText(),
                        voorraad
                    );       
          }

            private void addProduct(int prodId, int catId, String naam, double prijs, String omschrijv, int voorraad) {
               Product product = new Product(prodId, catId, naam, omschrijv, prijs, voorraad);
               
               WinkelApplication.getQueryManager().updateProduct(product);
               
               javax.swing.SwingUtilities.getWindowAncestor(SupplyEdit.this).dispose();
               WinkelApplication.getInstance().showPanel(new view.supply.SupplyPanel());
               
            }
        });
        
        panel.add(opslaan);        
        panel.add(terug);
        
        return panel;
    }
    
}
